<template>
  <div class="container">
    <div class="card">
      <div class="header">
        <div>审批设置</div>
        <el-pagination background layout="total, sizes, prev, pager, next"
                       :total="totalCount" :page-size="pageSize" :page-sizes="[50, 100, 150, 200, 250, 300]"
                       @size-change="handleSizeChange" @current-change="onCurrentClick"/>
      </div>
      <div class="body" v-loading="loading">
        <el-tree class="department" :data="deptTree" empty-text="请添加部门信息后再添加人员信息"
                 :props="defaultProps" highlight-current @node-click="handleNodeClick" />
        <el-transfer v-model="applyUserId" :data="personInfo" :props="transferProps"
                     :titles="transferTitles" style="height: 100%" @change="selectApprover"/>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import {onMounted, ref} from "vue";
import {IDept} from "@/iwb/IConfig";
import {getDepartment} from "@/api/config";
import {IUser} from "@/iwb/IUser";
import {useUserStore} from "@/store/user";

export default {
  name: "Apply",
  setup() {
    const pageIndex = ref(0);
    const pageSize = ref(50);
    const totalCount = ref(0);
    const userStore = useUserStore();
    const loading = ref(false);
    let currentDeptId = 0;
    const defaultProps = ref({
      children: 'children',
      label: 'name',
      value: 'id'
    })
    const deptTree = ref([] as Array<IDept>);

    const reloadDeptTree = () => {
      loading.value = true;
      getDepartment().then(res => {
        deptTree.value = res.data;
        loading.value = false;
      }).catch(error => {
        console.error(error);
        loading.value = false;
      })
    }
    const transferProps = ref({
      key: 'userId',
      label: 'username'
    })
    const transferTitles = ref(['人员列表', '审批人员']);
    const personInfo = ref([] as Array<IUser>);
    const applyUserId = ref([] as Array<string>);

    const handleNodeClick = (data: IDept, node: any) => {
      //console.log(data);
      //console.log(node);
      pageIndex.value = 0;
      totalCount.value = 0;
      const index = pageIndex.value;
      const size = pageSize.value;
      /**
       * 获取当前部门ID
       */
      currentDeptId = data.id;
      const userStore = useUserStore();
      userStore.queryUserByDeptId(pageIndex.value + 1, pageSize.value, currentDeptId).then((res: any) => {
        const {data} = res;
        totalCount.value = data.total;
        personInfo.value = data.users;
        const applyPerson = data.users.filter((item: any) => item.approver);
        applyPerson.forEach((item: IUser) => {
          applyUserId.value.push(item.userId + '');
        })
      })
    }

    const selectApprover = (nowValue: any, direction: string, keyValue: any) => {
      console.log(nowValue)
      console.log(direction);
      console.log(keyValue);
      loading.value = true;
      let enable = direction === 'right'
      console.log(enable);
      userStore.setUserApprover(keyValue, enable).then(()=> {
        loading.value = false;
      }).catch(error=> {
        loading.value = false;
        console.error(error);
      });
    }

    const handleSizeChange = (size: number) => {
      pageIndex.value = 0;
      totalCount.value = 0;
      const index = pageIndex.value;
      pageSize.value = size;
      const userStore = useUserStore();
      if (totalCount.value > index * size || pageIndex.value ===0) {
        userStore.queryUserByDeptId(pageIndex.value + 1, pageSize.value, currentDeptId).then((res: any) => {
          const {data} = res;
          totalCount.value = data.total;
          personInfo.value = data.users;
          const applyPerson = data.users.filter((item: any) => item.approver);
          applyPerson.forEach((item: IUser) => {
            applyUserId.value.push(item.userId + '');
          })
        })
      }
    }

    const onCurrentClick = (value: number) => {
      console.log(value);
      const index = value - 1;
      const size = pageSize.value;
      pageIndex.value = index;
      const userStore = useUserStore();
      if (totalCount.value > index * size || pageIndex.value ===0) {
        userStore.queryUserByDeptId(pageIndex.value + 1, pageSize.value, currentDeptId).then((res: any) => {
          const {data} = res;
          totalCount.value = data.total;
          personInfo.value = data.users;
          const applyPerson = data.users.filter((item: any) => item.approver);
          applyPerson.forEach((item: IUser) => {
            applyUserId.value.push(item.userId + '');
          })
        })
      }
    }

    onMounted(() => {
      reloadDeptTree();
    })

    return {
      pageSize,
      totalCount,
      loading,
      defaultProps,
      deptTree,
      personInfo,
      applyUserId,
      transferProps,
      transferTitles,
      handleNodeClick,
      selectApprover,
      handleSizeChange,
      onCurrentClick
    }
  }
}
</script>

<style scoped lang="scss">
@import "src/styles/variable";
.container {
  width: auto;
  height: 100%;
  padding: 10px;
  background-color: $color-background-base;
  border: 0;
  box-sizing: border-box;
}
.card {
  width: 100%;
  height: 100%;
  background-color: $color-sub-background-base;
  overflow: auto;
}
.header {
  padding: 10px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid $color-border-base;
}
.body {
  display: flex;
  flex-direction: row;
  margin: 20px;
  gap: 20px;
  height: calc(100% - 83px);

  .department {
    min-width: 200px;
  }
  .person {
    width: auto;
  }
  :deep(.el-tree--highlight-current .el-tree-node.is-current>.el-tree-node__content) {
    background-color: $color-primary-base;
    color: $color-text-base;
  }
}
:deep(.body) {
  .el-transfer-panel, .el-transfer-panel__body {
    height: 90%;
  }
}
</style>